﻿using System;
using System.Threading.Tasks;
using Chemistry.Models;
using Chemistry.Tools;
using GLCommon;

namespace Chemistry.ViewModels
{
    /// <summary>
    /// 输入准考证号vm
    /// </summary>
    public class InputNumberViewModel:VmBase
    {

        private bool _isButtonEnabled = true;
        public bool ButtonEnabled
        {
            get { return _isButtonEnabled; }
            set { _isButtonEnabled = value; RaisePropertyChanged("ButtonEnabled"); }
        }


        private string _number;
        /// <summary>
        /// 准考证号
        /// </summary>
        public string Number
        {
            get { return _number; }
            set { _number = value; RaisePropertyChanged("Number"); }
        }

        

        public InputNumberViewModel()
        {
            CommitCommand = new GalaSoft.MvvmLight.Command.RelayCommand(CommitExec,
                () => !string.IsNullOrEmpty(Number)&&_isButtonEnabled);

            Register(this, MessageToken.ShowInputNumber, (i) =>
            {
                _isButtonEnabled = true;
                this.Number = string.Empty;
                IsOpen = true;
            });

            Register<string>(this, MessageToken.InputNumberOver,   (i) =>
            {
                    this.Number = i;
                    CommitExec();
            });
        }

        private async void CommitExec()
        {
            _isButtonEnabled = false;
            await Execute();
            _isButtonEnabled = true;
        }

        private async Task Execute()
        {
            ExamInfo examInfo = await GetExaminee();
            if (examInfo != null)
            {
                Send<ExamInfo>(examInfo, MessageToken.ShowConfirId);
            }
           
        }

        private async Task<ExamInfo> GetExaminee()
        {
            ExamInfo examInfo = null;
            Send<string>("获取考试信息中......", MessageToken.ShowExamLoadMessage);
            string url = string.Format("oplogin?nr={0}&km=k2",Number);

            var result = await HttpTool.GetAsync<ExamineeRemote>(url);
            await Task.Delay(1000);
            Send<string>("获取考生信息中......", MessageToken.CloseExamLoadMessage);
            if (result != null)
            {
                string loginMsg = String.Empty;
                switch (result.status)
                {
                    case 0:
                        Examinee  examinee = new Examinee(){ Name =  result.name, TicketId = result.nr};
                        string info = string.Format("考生{0}登录", examinee.Name);
                        LogTool.WriteInfoLog(info);
                        RandomQeustion(examinee);
                        examInfo = new ExamInfo()
                        {
                             ExamineeRemote = result,
                              Examinee =  examinee
                        };
                        if (!string.IsNullOrEmpty(result.photo))
                            examinee.Pictrue = Convert.FromBase64String(result.photo);
                        IsOpen = false;
                        break;
                    case 1:
                        loginMsg = "准考证不存在";
                        break;
                    case 2:
                        loginMsg = "考试计划没有审批同意";
                        break;
                    case 3:
                        loginMsg = "考试时间不正确";
                        break;
                    case 4:
                        loginMsg = "触屏机自助考试没有完成";
                        break;
                    case 5:
                        loginMsg = "考试成绩已合格";
                        break;
                    case 6:
                        loginMsg = "考生补考未合格";
                        break;
                    case 7:
                        loginMsg = "已有k1/k2/k3成绩";
                        break;
                    case 8:
                        loginMsg = "未分配k1/k2/k3考试项";
                        break;
                    case -1:
                        loginMsg = "考试进度错误";
                        break;
                    case -3:
                        loginMsg = "服务器内部错误，请联系管理员";
                        break;
                }
                if (result.status != 0)
                {
                    await Task.Delay(1000);
                    Send<string>(loginMsg, MessageToken.ShowExamMessage);
                }
            }
            else
            {
                Send<string>("获取考试信息中......", MessageToken.CloseExamLoadMessage);
                await Task.Delay(1000);
                Send<string>("未找到考生信息", MessageToken.ShowExamMessage);
            }
            return examInfo;
        }

        private static void RandomQeustion(Examinee examinee)
        {
            if (Global.Examinees.Count > 10)
                Global.Examinees.Clear();
            if (Global.Examinees.Count == 0)
                Global.Random = new Random().Next(10);

            Global.Examinees.Add(examinee);

            if (Global.Examinees.Count - 1 == Global.Random)
            {
                var random = new Random().Next(3);
                switch (random)
                {
                    case 0:
                        examinee.IsLeakAbnormal = true;
                        break;
                    case 1:
                        examinee.IsPressureAbnormal = true;
                        break;
                    case 2:
                        examinee.IsTemperatureAbnormal = true;
                        break;
                }
            }

        }
    }
}
